package com.ruoyi.web.controller.module.asset;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.asset.EstateAssetInfo;
import com.ruoyi.system.domain.module.asset.EstateAssetPlan;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.estate.EstateUser;
import com.ruoyi.system.domain.module.property.Property;
import com.ruoyi.system.service.module.asset.IEstateAssetInfoService;
import com.ruoyi.system.service.module.asset.impl.IEstateAssetPlanService;
import com.ruoyi.system.service.module.estate.IEstateService;
import com.ruoyi.system.service.module.estate.IEstateUserService;
import com.ruoyi.system.service.module.property.IPropertyService;
import com.ruoyi.system.service.system.ISysUserService;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.web.controller.common.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 固定资产盘点任务Controller
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Validated
@Api(value = "固定资产盘点任务控制器", tags = {"固定资产盘点任务管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/assetPlan")
public class EstateAssetPlanController extends BaseController {

    private final IEstateAssetPlanService estateAssetPlanService;
	private final IPropertyService propertyService;
	private final IEstateService estateService;
	private final ISysUserService sysUserService;
	private final IEstateAssetInfoService estateAssetInfoService;
    /**
     * 查询固定资产盘点任务列表
     */
    @ApiOperation("查询固定资产盘点任务列表")
    @PreAuthorize("@ss.hasPermi('module:assetPlan:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateAssetPlan> list(@Validated EstateAssetPlan estateAssetPlan) {
        QueryWrapper<EstateAssetPlan> qw = new QueryWrapper<>();
		if (estateAssetPlan.getEstateId()!=null){
			qw.eq("estate_id",estateAssetPlan.getEstateId());
		}
		if (estateAssetPlan.getPropertyId()!=null){
			qw.eq("property_id",estateAssetPlan.getPropertyId());
		}
        TableDataInfo<EstateAssetPlan> td = PageUtils.buildDataInfo(estateAssetPlanService.page(PageUtils.buildPage(),qw));
        List<EstateAssetPlan> list = td.getRows();
		if (list.size()>0){
			for (EstateAssetPlan plan:list){
				if (plan.getPropertyId()!=null){
					Property property = propertyService.findById(plan.getPropertyId());
					if (property!=null){
						plan.setPropertyName(property.getPropertyName());
					}
				}
				if (plan.getEstateId()!=null){
					Estate estate = estateService.findById(plan.getEstateId());
					if (estate!=null){
						plan.setEstateName(estate.getEstateName());
					}
				}
				if (plan.getSysUserId()!=null){
					SysUser user = sysUserService.selectUserById(plan.getSysUserId());
					if (user!=null){
						plan.setSysUserName(user.getNickName());
					}
				}
			}
		}
        return td;
    }

    /**
     * 导出固定资产盘点任务列表
     */
    @ApiOperation("导出固定资产盘点任务列表")
    @PreAuthorize("@ss.hasPermi('module:assetPlan:export')")
    @Log(title = "固定资产盘点任务", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateAssetPlan> export(@Validated EstateAssetPlan estateAssetPlan) {
        List<EstateAssetPlan> list = estateAssetPlanService.list();
        ExcelUtil<EstateAssetPlan> util = new ExcelUtil<EstateAssetPlan>(EstateAssetPlan.class);
        return util.exportExcel(list, "固定资产盘点任务");
    }

    /**
     * 获取固定资产盘点任务详细信息
     */
    @ApiOperation("获取固定资产盘点任务详细信息")
    @PreAuthorize("@ss.hasPermi('module:assetPlan:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateAssetPlan> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateAssetPlan estateAssetPlan = estateAssetPlanService.getById(id);
		List<Integer> planInfo = new ArrayList<>();
		QueryWrapper<EstateAssetInfo> qw1 = new QueryWrapper<>();
		qw1.eq("plan_id",estateAssetPlan.getId());
		List<EstateAssetInfo> infoList = estateAssetInfoService.list(qw1);
		if (infoList.size()>0){
			for (EstateAssetInfo info:infoList){
				planInfo.add(Math.toIntExact(info.getAssetPointId()));
			}
		}
		estateAssetPlan.setPlanInfo(planInfo);
        return AjaxResult.success(estateAssetPlan);
    }

    /**
     * 新增固定资产盘点任务
     */
    @ApiOperation("新增固定资产盘点任务")
    @PreAuthorize("@ss.hasPermi('module:assetPlan:add')")
    @Log(title = "固定资产盘点任务", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateAssetPlan estateAssetPlan) {
		estateAssetPlan.setState(0L);
		Boolean b = estateAssetPlanService.save(estateAssetPlan);
		if (estateAssetPlan.getPlanInfo()!=null){
			for (Integer info:estateAssetPlan.getPlanInfo()){
				EstateAssetInfo infos = new EstateAssetInfo();
				infos.setAssetPointId(Long.valueOf(info));
				infos.setPlanId(estateAssetPlan.getId());
				b=estateAssetInfoService.save(infos);
			};
		}
		if (b){
			return toAjax(1);
		}else {
			return toAjax(0);
		}
    }

    /**
     * 修改固定资产盘点任务
     */
    @ApiOperation("修改固定资产盘点任务")
    @PreAuthorize("@ss.hasPermi('module:assetPlan:edit')")
    @Log(title = "固定资产盘点任务", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateAssetPlan estateAssetPlan) {
		QueryWrapper<EstateAssetInfo> qw = new QueryWrapper<>();
		qw.eq("plan_id",estateAssetPlan.getId());
		List<EstateAssetInfo> infoList = estateAssetInfoService.list(qw);
		if (infoList.size()>0){
			for (EstateAssetInfo info:infoList){
				estateAssetInfoService.removeById(info.getId());
			}
		}
		if (estateAssetPlan.getPlanInfo()!=null){
			for (Integer info:estateAssetPlan.getPlanInfo()){
				EstateAssetInfo infos = estateAssetInfoService.findById(info);
				infos.setAssetPointId(Long.valueOf(info));
				infos.setPlanId(estateAssetPlan.getId());
				Boolean b=estateAssetInfoService.save(infos);
			};
		}
        return toAjax(estateAssetPlanService.updateById(estateAssetPlan) ? 1 : 0);
    }

    /**
     * 删除固定资产盘点任务
     */
    @ApiOperation("删除固定资产盘点任务")
    @PreAuthorize("@ss.hasPermi('module:plan:remove')")
    @Log(title = "固定资产盘点任务" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateAssetPlanService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
